/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parsertojaklin;

import com.mysql.jdbc.Statement;
import java.io.File;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Юра
 */
public class Group  {
    public String table_name = "groups";
    static  File dir = new File("D:\\projects\\optvideo\\optvideo\\optvideo.com\\new_design\\new_kat\\katalog\\main_group\\");
//http://optvideo.com/new_design/new_kat/katalog.php?kategory=102532&grupp=%DD%CB%C5%CC%C5%CD%D2%DB%20%CF%C8%D2%C0%CD%C8%DF&tab=1
    public String id_groups;
    public String name_group;
    public String belong_group;
    public String visibility;
    public String meta_noindex;

    Group (){
    }

    Group(String id_group,  String name_group, String belong_group){
    this.id_groups = id_group;
    this.belong_group = belong_group;
    this.name_group = name_group;

    }

    public String insert_string() {
    String query =   "REPLACE INTO groups (`id_groups`, `name_groups`, `belong_groups`) VALUES";
    query += "('"+id_groups+"', '"+name_group+"', '"+belong_group+"')";

    return query;
    }

    public static  Hashtable<String, Group> parse_groups(Statement s) throws SQLException{
        Hashtable<String, Group> groups = new Hashtable<String, Group>();

            Matcher matcher = parser.find_in_file("php-kategory=(.{1,6})&grupp=(.{1,40}).htm", dir+"\\main_groups.txt");
            while(matcher.find()){
                System.out.println(matcher.group(1)+"_"+matcher.group(2));
                Group gr = new Group(matcher.group(1),matcher.group(2),"0");
                groups.put(gr.id_groups, gr);
            }

            String[] dir_list = dir.list();

            int c=0;
            //String substr = "view.php-kod=";   // wares
            //String substr = "news_vybrat";     // stat`i
            String file_name = "katalog.php-kategory=";  // vendors



            for (String filename :dir_list)
                if (filename.length()>=file_name.length() && filename.substring(0,file_name.length()).equals(file_name) )
                {
                    String parent = filename.substring(file_name.length(),filename.indexOf("&grupp="));
                    //<a href="katalog.php-grupp_kod=
                    parser.find_cupl(".*<a href=\"katalog.php-grupp_kod=(.{2,7})&otobr=.*class=\"link4_1\">(.{1,32})</a>.*", dir+"\\"+filename, groups, parent);
                    
                    //s.executeUpdate(group.insert_string());
                    

                    if ( c++>600) break;
                }
        return groups;
    }
}
